package com.yixun.qfbao.hander;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.api.service.InitDataService;
import com.yixun.qfbao.apidto.InitDataReqDto;
import com.yixun.qfbao.apidto.InitDataResDto;
import com.yixun.qfbao.apidto.SendWXMsgAgainReqDto;
import com.yixun.qfbao.apidto.SendWXMsgAgainResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.InitTypeEnum;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.InitDataReqVo;
import com.yixun.qfbao.vo.SendWXMsgAgainReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import net.sf.ehcache.CacheManager;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 补发微信消息
 *
 * @Description 获取楼盘信息
 * @Date 2018/1/10
 * @Author sangtao
 * @Computer Administrator
 */
@Service("sendWXMsgAgainHander")
public class SendWXMsgAgainHander extends CommonAbstractHander {
    private Logger logger = LoggerFactory.getLogger(SendWXMsgAgainHander.class);

    @Autowired
    private InitDataService initDataService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private LoginUtils loginUtils;
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    WxAccessTokenCacheUtils wxAccessTokenCacheUtils;

    @SuppressWarnings("rawtypes")
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        SendWXMsgAgainReqVo req = (SendWXMsgAgainReqVo)reqDto;
        SendWXMsgAgainReqDto reqDto1 = req.getParams();
        BaseResponseDTO<SendWXMsgAgainResDto> res = new BaseResponseDTO<SendWXMsgAgainResDto>();
        SendWXMsgAgainResDto resDto = new SendWXMsgAgainResDto();
        try {
            String assistantAccessToken = wxAccessTokenCacheUtils.getAccessToken(reqDto1.getCompanyUuid(), SystemIdEnum.ASSISTANT.getCode(), null);
            initDataService.sendWXMsgAgain(reqDto1, assistantAccessToken);
        } catch (Exception e) {
            logger.error("SendWXMsgAgainHander ==> error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setErrorCode("补发失败");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }
}
